import os

import edge_tts

from ai_core.tts.base import Tts
from ai_core.utils.util import Util


class Edge_TTS(Tts):
    # def __init__(self):
    #     self.voice = "zh-CN-XiaoxiaoNeural"

    def __init__(self,config): #
        self.output_dir = Util.get_project_dir() + config.get("output_dir")
        self.voice = config.get("voice")

    async def text_to_opus_data(self, text, format_str="mp3"):
        audio_path = Util.get_random_file_path(self.output_dir, format_str)
        communicate = edge_tts.Communicate(text, self.voice)
        await communicate.save(audio_path)
        # 判断是否生成mp3文件
        if not os.path.exists(audio_path):
            return None
        return super().audio_file_to_opus(audio_path)
